<template>
  <div class="app-container  bg-gray">
    <el-row class="box-1">
      <el-col :span="24">
        <el-card>
          <div class="header">
            <div>
              <label style="font-size: 15px;color: #b7b7b7;">注: 最多添加20个产品类型</label>
              <!--<el-input v-model="searchTerms.searchValue" placeholder="请输入内容" class="search-input" @change="getData" />-->
              <!--<el-button type="success" class="bg-green" icon="el-icon-search" @click="getData">搜索</el-button>-->
            </div>
            <el-button type="success" class="bg-green" icon="el-icon-circle-plus" @click="toAdd">新增产品类型</el-button>
          </div>

          <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
            <el-table-column prop="id" label="ID" width="100" header-align="center" align="center" sortable />
            <el-table-column prop="name" label="产品类型名称" min-width="160"/>
            <el-table-column prop="created_at" label="创建时间" min-width="160"/>
            <el-table-column label="操作" width="240" header-align="center" align="center">
              <template slot-scope="scope">
                <el-button type="success" size="small" icon="el-icon-edit" circle class="bg-green" @click="toEdit(scope.row)"/>
                <el-button type="danger" size="small" icon="el-icon-delete" circle @click="toDelete(scope.row)" />
              </template>
            </el-table-column>
          </el-table>
          <!--<el-pagination
            :current-page.sync="currentPage"
            :page-size="pageSize"
            :page-sizes="pageSizes"
            :total="total"
            class="pagination"
            layout="total, sizes, prev, pager, next, jumper"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"/>-->
        </el-card>
      </el-col>

      <el-dialog :visible.sync="dialogVisible" :title="form.title" width="500px" class="dialog" center @close="resetDialog('form')">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" autofocus/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitDialog('form')">确 定</el-button>
        </div>
      </el-dialog>
    </el-row>
  </div>
</template>

<script>
import { handleSortChange, handleSizeChange, handleCurrentChange } from '@/utils/table'
export default {
  data() {
    return {
      searchTerms: {
        searchValue: ''
      },

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 380,
      tableLoading: false,
      multipleSelection: [],
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: [],

      // 弹出框相关数据
      dialogVisible: false,
      form: {
        title: '',
        name: ''
      },
      rules: {
        name: [{ required: true, message: '请输入产品类型名称', trigger: 'change' }]
      }
    }
  },
  watch: {
  },
  created() {
    this.getData()
  },
  methods: {
    handleSortChange, handleSizeChange, handleCurrentChange,
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'goods/goodsTypeList'
      const data = {
        page_no: this.currentPage,
        page_size: this.pageSize,
        search: this.searchTerms.searchValue
      }
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data
        this.total = res.data.total
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    toDelete(row) {
      this.$confirm('此操作将删除该产品类型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = '/goods/delGoodsType/' + row.id
        this.$store.dispatch('GetConnect', { url }).then(res => {
          this.$message.success(res.msg)
          this.getData()
        }).catch(e => {
          this.$message.error(e.msg + ',请刷新或联系管理员!')
        })
      })
    },

    toAdd() {
      this.form = {}
      this.dialogVisible = true
      this.form.title = '添加产品类型'
    },
    toEdit(row) {
      this.form = {}
      this.dialogVisible = true
      this.form = {
        id: row.id,
        name: row.name
      }
      this.form.title = '编辑产品类型'
    },
    /**
     * 弹窗表单提交方法,通过判断有没有form_id执行添加或编辑操作
     * formName: form表单中ref的值,即表单名
     */
    submitDialog(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const url = this.form.id ? '/goods/updateGoodsType' : '/goods/addGoodsLype'
          const data = this.form
          this.$store.dispatch('GetConnect', { url, data }).then(res => {
            this.dialogVisible = false
            this.$message.success(res.msg)
            this.getData()
          }).catch(e => {
            this.$message.error(e.msg + ',请刷新或联系管理员!')
          })
        } else {
          this.$message.error('提交失败,请检查必填项')
        }
      })
    },
    resetDialog(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../styles/index.scss';
.box-1 {
  .header {
    @include flex-row(center, space-between);
    padding-bottom: 15px;
    .search-input {
      width: 240px;
    }
  }
}

.dialog {
  .el-input {
    width: 80%;
  }
}
</style>
